package cn.gson.springboot.controller;


import cn.gson.springboot.model.pojos.Product;
import cn.gson.springboot.model.pojos.ProductClassify;
import cn.gson.springboot.model.service.ProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ProductController {
    @Autowired
    ProductService productService;

    @RequestMapping("/product-list")
    @ResponseBody
    public Map<String,Object> productList(Integer page,Integer size, String search,Integer type) {
         Page<Object> obj = PageHelper.startPage(page, size);
        List<Product> products = productService.selectAll(search,type);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());//返回总页数
        map.put("rows",products);//返回查到的数据
        return map;
    }
    @RequestMapping("/product-classify-list")
    @ResponseBody
    public List<ProductClassify> classifyList() {
        List<ProductClassify> classify = productService.classify();
        return classify;
    }

    @RequestMapping("/product-deleteOne")
    @ResponseBody
    public void deleteOne() {
        productService.deleteOne(2);
    }

   @RequestMapping("/product-selectOne")
    @ResponseBody
    public Product selectOne(Integer productId) {
        Product product = productService.selectOne(productId);
        return product;
    }

    @RequestMapping("/product-selectPros")
    @ResponseBody
    public List<Product> selectPros(Integer productClassifyId) {
        List<Product> products = productService.selectPros(productClassifyId);
        return products;
    }

    @RequestMapping("/product-alter")
    @ResponseBody
    public void alter() {
        Product product = new Product();
        product.setProductName("校服");
        productService.alter(1,product);

    }

    @RequestMapping("/product-addProduct")
    @ResponseBody
    public void addProduct(@RequestBody Product product) {
        System.out.println(product);
        productService.addProduct(product);
    }
}